package com.zwy.leetcode;

import java.util.*;

public class StringTest {
    public static void main(String[] args) {
        StringTest test=new StringTest();

        String s="A man, a plan, a canal: Panama";
        test.isPalindrome(s);

    }
    /**
     * 反转字符串
     * @param s
     */
    public void reverseString(char[] s) {
        char t;
        for (int i = 0; i < s.length/2; i++) {
            t=s[i];
            s[i]=s[s.length-i-1];
            s[s.length-i-1]=t;
        }
    }

    /**
     * 整数反转
     * @param x
     * @return
     */
    public int reverse(int x) {
        boolean flag=true;
        if(x<0){
            flag=false;
            x=-x;
        }
        String s=x+"";
        char[] chars = s.toCharArray();
        char t;
        for (int i = 0; i < chars.length/2; i++) {
            t=chars[chars.length-1-i];
            chars[chars.length-1-i]=chars[i];
            chars[i]=t;
        }
        s= String.valueOf(chars);
        int i=0;
        try{
            i = Integer.parseInt(s);
        }catch (Exception e){
            return 0;
        }

        if(flag)return i;
        return -i;

    }

    /**
     * 字符串中的第一个唯一字符
     * @param s
     * @return
     */
    public int firstUniqChar(String s) {
        Map<Character,Integer> map=new LinkedHashMap<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(map.get(chars[i])==null){
                map.put(chars[i],i);
            }else {
                map.put(chars[i],-1);
            }
        }
        Set<Character> set = map.keySet();
        for (Character character : set) {
            Integer integer = map.get(character);
            if(integer!=-1)
                return integer.intValue();
        }
        return -1;
    }

    /**
     * 有效的字母异位词
     * @param s
     * @param t
     * @return
     */
    public boolean isAnagram(String s, String t) {
        //特判，长度不同必定不是
        if (s.length()!=t.length()) return false;
        //两个字符串排序后是否相等
        char[] array1 = s.toCharArray();
        char[] array2 = t.toCharArray();
        Arrays.sort(array1);
        Arrays.sort(array2);
        return Arrays.equals(array1,array2);
    }

    /**
     * 验证回文串
     * @param s
     * @return
     */
    public boolean isPalindrome(String s) {
        if (s.length() == 0)
            return true;
        int left = 0, right = s.length() - 1;
        while (left < right) {
            //因为题中说了，只考虑字母和数字，所以不是字母和数字的先过滤掉
            while (left < right && !Character.isLetterOrDigit(s.charAt(left)))
                left++;
            while (left < right && !Character.isLetterOrDigit(s.charAt(right)))
                right--;
            //然后把两个字符变为小写，在判断是否一样，如果不一样，直接返回false
            if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right)))
                return false;
            left++;
            right--;
        }
        return true;

    }

    /**
     * 实现 strStr()
     * @param haystack
     * @param needle
     * @return
     */
    public int strStr(String haystack, String needle) {
        return 0;
    }
}
